package com.hsgene.generator.util;

import com.hsgene.generator.constants.GeneratorConstants;
import com.hsgene.generator.constants.GeneratorEnum;
import com.hsgene.generator.util.generator.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * ID生成器工厂
 *
 * @author wangbing
 * @version 1.0, 2018/6/29
 */
@Component
public class GeneratorFactory {

    @Autowired
    private GeneratorConstants generatorConstants;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 获取ID/编号生成器
     *
     * @param generatorType ID/编号生成器类型
     * @return 生成器
     */
    public AbstractGenerator getGenerator(GeneratorEnum generatorType) {
        AbstractGenerator generator;
        switch (generatorType) {
            case SMS:
                generator = SmsGenerator.getInstance(generatorConstants.getSmsVerifyCodeLength());
                break;
            case USER:
                generator = UserGenerator.getInstance();
                break;
            case ORDER:
                generator = OrderGenerator.getInstance();
                break;
            case GOODS:
                generator = GoodsGenerator.getInstance();
                break;
            case PAY:
                generator = PayGenerator.getInstance();
                break;
            case MATERIAL_APPLY:
                generator = MaterialApplyGenerator.getInstance();
                break;
            case MATERIAL_STORAGE:
                generator = MaterialStorageGenerator.getInstance();
                break;
            case DEFAULT:
            case UNDEFINED:
            default:
                generator = DefaultGenerator.getInstance();
        }
        generator.setRedisTemplate(stringRedisTemplate);
        return generator;
    }
}
